﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.LogEntries.ChangeSettlementOwnerLogEntry
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Localization;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.LogEntries
{
  public class ChangeSettlementOwnerLogEntry : LogEntry, IEncyclopediaLog, IWarLog
  {
    [SaveableField(80)]
    public readonly Settlement Settlement;
    [SaveableField(81)]
    public readonly Clan PreviousClan;
    [SaveableField(82)]
    public readonly Clan NewClan;
    [SaveableField(83)]
    private readonly bool _bySiege;

    internal static void AutoGeneratedStaticCollectObjectsChangeSettlementOwnerLogEntry(
      object o,
      List<object> collectedObjects)
    {
      ((LogEntry) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Settlement);
      collectedObjects.Add((object) this.PreviousClan);
      collectedObjects.Add((object) this.NewClan);
    }

    internal static object AutoGeneratedGetMemberValueSettlement(object o)
    {
      return (object) ((ChangeSettlementOwnerLogEntry) o).Settlement;
    }

    internal static object AutoGeneratedGetMemberValuePreviousClan(object o)
    {
      return (object) ((ChangeSettlementOwnerLogEntry) o).PreviousClan;
    }

    internal static object AutoGeneratedGetMemberValueNewClan(object o)
    {
      return (object) ((ChangeSettlementOwnerLogEntry) o).NewClan;
    }

    internal static object AutoGeneratedGetMemberValue_bySiege(object o)
    {
      return (object) ((ChangeSettlementOwnerLogEntry) o)._bySiege;
    }

    public ChangeSettlementOwnerLogEntry(
      Settlement settlement,
      Hero newOwner,
      Hero previousOwner,
      bool bySiege)
    {
      this.Settlement = settlement;
      this.PreviousClan = previousOwner.Clan;
      this.NewClan = newOwner.Clan;
      this._bySiege = bySiege;
    }

    public override ImportanceEnum GetImportanceForClan(Clan clan)
    {
      if (this.PreviousClan == this.NewClan)
        return ImportanceEnum.Zero;
      return this.NewClan != clan ? ImportanceEnum.Important : ImportanceEnum.VeryImportant;
    }

    public bool IsRelatedToWar(StanceLink stance, out IFaction effector, out IFaction effected)
    {
      IFaction faction1 = stance.Faction1;
      IFaction faction2 = stance.Faction2;
      effector = this.NewClan.MapFaction;
      effected = this.PreviousClan.MapFaction;
      if (this.NewClan.MapFaction == faction1 && this.PreviousClan.MapFaction == faction2)
        return true;
      return this.NewClan.MapFaction == faction2 && this.PreviousClan.MapFaction == faction1;
    }

    public override void GetConversationScoreAndComment(
      Hero talkTroop,
      bool findString,
      out string comment,
      out ImportanceEnum score)
    {
      score = ImportanceEnum.Zero;
      comment = "";
      if (!this._bySiege || this.NewClan != Clan.PlayerClan && this.PreviousClan != Clan.PlayerClan)
        return;
      score = ImportanceEnum.Important;
      if (!findString)
        return;
      comment = "str_comment_changeownerofsettlement_you_captured_castle";
    }

    public override int GetAsRumor(Settlement talkSettlement, ref TextObject comment)
    {
      int asRumor = 0;
      Settlement settlement = this.Settlement;
      if (this.NewClan.IsBanditFaction && settlement.IsHideout && Campaign.Current.Models.MapDistanceModel.GetDistance(talkSettlement, settlement, 60f, out float _))
      {
        comment = new TextObject("{=MXGtQ6YV}I hear {.%}{BANDIT_NAME}{.%} have moved into the old {HIDEOUT_NAME} near here. Travellers better watch themselves.");
        comment.SetTextVariable("BANDIT_NAME", this.NewClan.Name);
        comment.SetTextVariable("HIDEOUT_NAME", settlement.Name);
        return 4;
      }
      if (!this._bySiege || this.PreviousClan != talkSettlement.MapFaction)
        return asRumor;
      comment = new TextObject("{=UMn2QMIk}Did you hear {ENEMY_NAME} took {FORTRESS_NAME} by storm? Do you think they'll come here next?");
      comment.SetTextVariable("ENEMY_NAME", FactionHelper.GetTermUsedByOtherFaction((IFaction) this.NewClan, talkSettlement.MapFaction, false));
      comment.SetTextVariable("FORTRESS_NAME", settlement.Name);
      return 10;
    }

    public override string ToString() => this.GetEncyclopediaText().ToString();

    public bool IsVisibleInEncyclopediaPageOf<T>(T obj) where T : MBObjectBase
    {
      return ((object) obj == this.Settlement || (object) obj == this.NewClan || (object) obj == this.NewClan.Leader) && !this._bySiege;
    }

    public TextObject GetEncyclopediaText()
    {
      TextObject text = GameTexts.FindText("str_settlement_owner_changed_news");
      text.SetTextVariable("SETTLEMENT", this.Settlement.IsHideout ? this.Settlement.Name : this.Settlement.EncyclopediaLinkWithName);
      if (this.NewClan == null && this.Settlement.IsHideout)
        return GameTexts.FindText("str_hideout_owner_changed_news");
      StringHelpers.SetCharacterProperties("LORD", this.NewClan.Leader.CharacterObject, text);
      return text;
    }
  }
}
